<template>
    <div>
        <!--健康指导弹框 start-->
        <el-dialog class="dialog-frame inspection-dialog" width="600" :visible.sync="healthDataDialog" @close="closeDialog('healthDataDialog')" :close-on-click-modal="false" :close-on-press-escape="false">
            <div slot="title" class="title-size">
                <div class="title-icon">
                    <img src="@/assets/img/img_decorate.png" alt="">
                </div>
                <span>健康指导意见单</span>
            </div>
            <div class="inspection-left">
                <el-tabs v-model="healthActiveName" @tab-click="handleHealthClick">
                    <el-tab-pane label="公用模板" name="0">
                    </el-tab-pane>
                    <el-tab-pane label="个人模板" name="1">
                    </el-tab-pane>
                </el-tabs>
                <div style="text-align:left;margin-bottom:15px;">
                    <el-input style="width: 200px;display: inline-block;" size="small" v-model="healthSearch" placeholder="请输入模板名称进行搜索"></el-input>
                    <el-button type="primary" size="small" @click="getHealthList">搜 索</el-button>
                </div>
                <el-table class="l-table" :data="healthDataList" border max-height="300" highlight-current-row>
                    <el-table-column prop="diagnosisName" label="诊断名称">
                        <template slot-scope="scope">
                            <span>{{scope.row.diagnosisName || '-'}} </span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="guidanceName" label="模板名称">
                        <template slot-scope="scope">
                            <span>{{scope.row.guidanceName || '-'}} </span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <p class="table-btn-size" @click="quoteHealthModel(scope.row)">引用意见单</p>
                            <span class="table-btn-size" @click="checkHealthModel(scope.row)">查看模板</span>
                            <span v-if="healthActiveName != '0'" class="table-btn-size table-btn-del" @click="delectHealthModel(scope.row)">删除</span>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="inspection-right">
                <div>
                    <h3 class="inspection-title">诊断</h3>
                    <div class="dialog-content" style="min-height: 50px;text-align:left;">
                        <span class="tag-name-group" v-if="diagnosisArray.length > 0">
                            <span v-for="(item, index) in diagnosisArray" :key="index">
                                <span v-if="index !== 0">,</span>
                                {{item.diagnosis}}
                            </span>
                        </span>
                        <span v-else>暂无诊断</span>
                    </div>
                </div>
                <div>
                    <h3 class="inspection-title">医生健康指导</h3>
                    <el-input style="margin:15px 0;" v-model="doctorHealthGuide" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入您的意见"></el-input>
                    <div style="text-align:right;">
                        <el-button type="primary" plain size="small" @click="openSaveAnotherAdvice">另存模板</el-button>
                        <el-button type="primary" size="small" @click="createAdvice" :loading="creatLoading">生成指导意见单</el-button>
                    </div>
                </div>
                <div>
                    <h3 class="inspection-title">已指导意见单</h3>
                    <div class="inspection-content" v-for="(item,index) in healthGuidanceList" :key="index">
                        <p>
                            意见单编号：{{item.healthGuidanceNo}}
                        </p>
                        <p>
                            <span class="table-btn-size" @click="check(item)">查看意见单</span>
                            <!-- <span class="table-btn-size" @click="openSaveAnotherAdvice">另存模板</span> -->
                            <span class="table-btn-size table-btn-del" @click="deleteInquiryLogHealthGuidance(item)">删除</span>
                        </p>
                    </div>
                </div>
            </div>
        </el-dialog>
        <!--健康指导--查看模板弹框-->
        <el-dialog class="dialog-frame" width="600" :visible.sync="healthDetailsDialog" @close="closeDialog('healthDetailsDialog')" :close-on-click-modal="false" :close-on-press-escape="false">
            <div slot="title" class="title-size">
                <div class="title-icon">
                    <img src="@/assets/img/img_decorate.png" alt="">
                </div>
                <span>模板内容</span>
            </div>
            <div class="dialog-content">
                {{healthDetailsInput}}
            </div>
            <div class="dialog-btn">
                <el-button type="primary" plain size="small" @click="closeDialog('healthDetailsDialog')">关 闭</el-button>
            </div>
        </el-dialog>
        <!--健康指导--查看模板弹框 end -->
        <!--健康指导--另存意见单弹框-->
        <el-dialog class="dialog-frame" width="600" :visible.sync="healthSaveDialog" @close="closeDialog('healthSaveDialog')" :close-on-click-modal="false" :close-on-press-escape="false">
            <div slot="title" class="title-size">
                <div class="title-icon">
                    <img src="@/assets/img/img_decorate.png" alt="">
                </div>
                <span>另存健康指导意见单模板</span>
            </div>
            <div style="text-align:center;">
                <div>
                    <span style="color:#fa6464;margin:0 5px 0 2px;">*</span>诊断名称
                    <el-input size="small" style="width: 220px;;margin-left:10px;" v-model="healthSaveICDInput" :disabled="true"></el-input>
                    <el-button type="primary" size="small" @click="openICDDialog('health')">ICD检索</el-button>
                    <p style="color:#fa6464;margin:5px 10px 10px 10px;">
                        提示：通过ICD检索与处方模板进行关联。
                    </p>
                </div>
                <div>
                    <span style="color:#fa6464;margin:0 5px 0 2px">*</span>模板名称
                    <el-input v-model="healthSaveInput" style="width:300px;margin-left:10px;" size="small"></el-input>
                </div>
                <el-button style="margin-top:20px;" type="primary" size="small" @click="saveAnotherAdvice">添加</el-button>
            </div>
        </el-dialog>
        <!--健康指导--另存意见单弹框 end -->
        <!--健康指导--选择ICD检索与处方模板 start -->
        <el-dialog class="dialog-frame dialog-choose" width="600" :visible.sync="chooseICDSearchDialog" @close="closeDialog('chooseICDSearchDialog')" :close-on-click-modal="false" :close-on-press-escape="false">
            <div slot="title" class="title-size">
                <div class="title-icon">
                    <img src="@/assets/img/img_decorate.png" alt="">
                </div>
                <span>选择ICD检索与处方模板</span>
            </div>
            <div class="choose-ICDSearch">
                <div>
                    <span>ICD检索</span>
                    <el-input class="ICD-Search" size="small" v-model="healthICDSearch"></el-input>
                    <el-button type="primary" size="small" @click="getICDList">搜索</el-button>
                </div>
                <span>检索列表</span>
                <div class="dialog-content ICDSearch-list">
                    <p v-for="(item, index) in ICDlist" :key="index" @click="saveICDSearch(item)">{{item.icdName}}</p>
                </div>
                <div style="color:#fa6464;">提示：点击检索列表进行选择</div>
            </div>
        </el-dialog>
        <!--健康指导--选择ICD检索与处方模板 end -->
        <!--健康指导弹框 end -->

        <pdf-dialog ref="pdfDialog" :has-save="false"></pdf-dialog>
    </div>
</template>

<script>
    import pdfDialog from '@/components/chatroom/view-pdf';
    export default {
        components: {
            pdfDialog
        },
        data() {
            return {
                logId: '',
                /* 健康指导弹框 start */
                healthDataDialog: false,
                healthActiveName: '0', //健康指导tab
                healthSearch: '', //健康指导搜索
                healthDataList: [], //健康指导列表
                healthSaveInput: '', //健康指导--新增个人模板名称input
                healthSaveICDInput: '', //健康指导--新增个人模板诊断input
                healthSaveICDId: null, //健康指导--新增个人模板诊断ID
                healthDetailsInput: '', //健康指导--查看模板input
                viewInquiryLog: [], //健康指导--查看意见单内容
                healthGuidanceList: [], //健康指导--问诊过程中生成的意见单
                doctorHealthGuide: '', //健康指导--医生健康指导内容
                recordEntityData: {
                    icdName: " ",
                    icdName2: " ",
                },
                healthICDSearch: '', //健康指导--icd检索
                ICDlist: [], //icd列表
                healthDetailsDialog: false, //健康指导弹框--查看模板
                healthSaveDialog: false, //健康指导弹框--另存意见单
                chooseICDSearchDialog: false, //健康指导弹框--另存意见单
                diagnosisArray: [], //诊断
                creatLoading: false, //加载
                /* 健康指导弹框 end */
            }
        },
        methods: {
            // 打开弹框
            onOpen(logId, array) {
                this.logId = logId;
                this.diagnosisArray = array;
                this.healthDataDialog = true;
                this.healthActiveName = '0';
                this.doctorHealthGuide = '';
                this.getHealthList();
                this.getInquiryLogHealthGuidanceList();
            },
            handleHealthClick(tab, event) { //健康指导模板tab
                this.healthActiveName = tab.name
                this.getHealthList()
            },
            getHealthList() { // 获取健康指导模板列表
                let para = {
                    webToken: this.$authInfo.webToken(),
                    modelType: this.healthActiveName,
                }
                if (this.healthSearch != '') para.guidanceName = this.healthSearch
                this.$ajax.getHealthGuidanceModelList(para).then(res => {
                    this.healthDataList = res.data.list || []
                }).catch(err => {

                })
            },
            getICDList() { //获取ICD列表
                this.ICDLoading = true
                let para = {
                    webToken: this.$authInfo.webToken(),
                }
                if (this.healthICDSearch) para.keyword = this.healthICDSearch
                this.$ajax.getIcdList(para).then(res => {
                    this.ICDlist = res.data.list || [];
                }).catch(err => {
                    this.$message({
                        message: err.msg,
                        type: 'error'
                    })
                })
            },
            quoteHealthModel(row) { //健康指导模板--引用意见单
                let para = {
                    webToken: this.$authInfo.webToken(),
                    id: row.id
                }
                this.$ajax.getHealthGuidanceModelDetail(para).then(res => {
                    this.doctorHealthGuide = res.data.guidanceContent
                }).catch(err => {
                    this.$message({
                        message: err.msg,
                        type: 'error',
                    })
                })
            },
            checkHealthModel(row) { //健康指导模板--查看模板&引用意见单
                this.healthDetailsDialog = true
                let para = {
                    webToken: this.$authInfo.webToken(),
                    id: row.id
                }
                this.$ajax.getHealthGuidanceModelDetail(para).then(res => {
                    this.healthDetailsInput = res.data.guidanceContent
                }).catch(err => {
                    this.$message({
                        message: err.msg,
                        type: 'error',
                    })
                })
            },
            delectHealthModel(row) { //健康指导模板--删除
                this.$confirm(`是否确认删除个人模板？`, '删除', {
                    customClass: 'l-confirm',
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(() => {
                    this.$ajax.delHealthGuidanceModel({ webToken: this.$authInfo.webToken(), id: row.id, }).then(res => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                        });
                        this.getHealthList();
                    }).catch(err => {
                        this.$message({
                            message: err.msg,
                            type: 'error',
                        })
                    })
                }).catch(() => {})
            },
            createAdvice() { //健康指导模板--生成意见单
                this.$message.closeAll();
                this.creatLoading = true
                let para = {
                    webToken: this.$authInfo.webToken(),
                    guidanceContent: this.doctorHealthGuide,
                    logId: this.logId,
                }
                this.$ajax.addInquiryLogHealthGuidance(para).then(res => {
                    this.$message({
                        message: res.msg,
                        type: 'success',
                    })
                    this.creatLoading = false
                    this.getInquiryLogHealthGuidanceList();
                    this.doctorHealthGuide = ''
                }).catch(err => {
                    this.creatLoading = false
                    this.$message({
                        message: err.msg,
                        type: 'error',
                    })
                })
            },
            deleteInquiryLogHealthGuidance(item) { //健康指导模板--作废意见单
                this.$message.closeAll();
                this.$confirm(`是否确认删除意见单？`, '删除', {
                    customClass: 'l-confirm',
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                }).then(() => {
                    this.$ajax.deleteInquiryLogHealthGuidance({ webToken: this.$authInfo.webToken(), id: item.id, }).then(res => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                        })
                        this.getInquiryLogHealthGuidanceList();
                    }).catch(err => {
                        this.$message({
                            message: err.msg,
                            type: 'error',
                        })
                    })
                }).catch(() => {})
            },
            openICDDialog() { //
                this.chooseICDSearchDialog = true;
            },
            saveICDSearch(item) { //选择ICD检索与处方模板--点击列表
                this.healthSaveICDInput = item.icdName;
                this.healthSaveICDId = item.id;

                this.chooseICDSearchDialog = false;
            },
            openSaveAnotherAdvice() { //打开健康指导模板--新增个人模板、另存模板
                if (this.doctorHealthGuide && this.doctorHealthGuide != '') {
                    this.healthSaveDialog = true;
                    this.healthSaveInput = '';
                    this.healthSaveICDId = null;
                    this.healthSaveICDInput = '';
                    this.healthICDSearch = '';
                    this.ICDlist = [];
                } else {
                    this.$message({
                        message: '请输入你的意见',
                        type: 'error',
                    })
                }
            },
            saveAnotherAdvice() { //健康指导模板--新增个人模板、另存模板
                let para = {
                    webToken: this.$authInfo.webToken(),
                    diagnosisId: this.healthSaveICDId,
                    guidanceContent: this.doctorHealthGuide,
                    guidanceName: this.healthSaveInput,
                }
                this.$ajax.addHealthGuidanceModel(para).then(res => {
                    this.$message({
                        message: res.msg,
                        type: 'success',
                    });
                    this.healthSaveDialog = false;
                    this.getHealthList();
                }).catch(err => {
                    this.$message({
                        message: err.msg,
                        type: 'error',
                    })
                })
            },
            getInquiryLogHealthGuidanceList(row) { //健康指导模板--获取问诊过程中已开健康指导意见单列表
                let para = {
                    webToken: this.$authInfo.webToken(),
                    logId: this.logId,
                }
                this.$ajax.getInquiryLogHealthGuidanceList(para).then(res => {
                    this.healthGuidanceList = res.data.list
                }).catch(err => {
                    this.$message({
                        message: err.msg,
                        type: 'error',
                    })
                })
            },
            closeDialog(dialogName) {
                if (dialogName == 'healthDataDialog') { //健康指导
                    this.healthDataDialog = false; // 关闭弹框

                    this.$emit('closed');
                }
                if (dialogName == 'healthDetailsDialog') { //健康指导--查看模板
                    this.healthDetailsDialog = false;
                }
                if (dialogName == 'healthSaveDialog') { //健康指导--另存意见单
                    this.healthSaveDialog = false;
                }
                if (dialogName == 'chooseICDSearchDialog') { //健康指导--选择ICD检索
                    this.chooseICDSearchDialog = false;
                }
            },
            check(val) {
                let params = {
                    id: val.id,
                    resource: 2
                };

                this.$refs.pdfDialog && this.$refs.pdfDialog.onOpen(params);
            },
        },
        mounted() {}
    }
</script>

<style lang="scss" src="@/style/page/vedio.scss"></style>